﻿CREATE FUNCTION dbo.fverTree_Parents
	( @LoanID int,
		@VerID int
	)
RETURNS @VerIDs TABLE (ID int,Checked bit,Lable sysname,Pos int)
AS
BEGIN

DECLARE @VerTree TABLE(Pos int IDENTITY,ID int,ParentID int)
INSERT @VerTree 
SELECT DISTINCT ID,ParentID FROM dbo.v_Loan_Verification WHERE LoanID = @LoanID

WHILE ISNULL(@VerID,0) <> 0
BEGIN

	INSERT @VerIDs
	SELECT VT.ID,LV.Checked,Lable,VT.Pos
	FROM @VerTree VT, v_Loan_Verification LV WITH(NOLOCK)
	WHERE VT.ID = @VerID AND LV.LoanID=@LoanID AND LV.ID=@VerID

	SELECT @VerID = ParentID FROM @VerTree WHERE ID = @VerID

END

RETURN

END
